from settings.config import *
import os
import chardet


# 打开文件的初始化操作
def openFileInit(frame, path):
    # 设置标题
    frame.SetTitle(APP_TITLE + ' - [' + path + ']')
    # 获取编码格式
    encoding = chardet.detect(open(path, 'rb').read(10*1024))
    print(encoding)
    # 建立历史记录
    # TODO 使用 sqlite 处理历史记录
    return loadFile(path, encoding)


# 载入文件
def loadFile(path, encoding):
    text_object = open(path, 'r', encoding=encoding['encoding'])

    while True:
        # yield处理大文件
        data = text_object.read(800)
        if not data:
            break
        yield data

# 清除文本中的空行
def clearTxtBlankLines(data_buffer):
    returnStr =  "".join([s for s in data_buffer.splitlines(True) if s.strip()])
    return returnStr

# 获取桌面路径
def get_desk_p():
    return os.path.join(os.path.expanduser('~'),"Desktop")

